/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import systole.domain.persons.identityCard.IdentityCardType;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.IdentityCardTypeSyncBroker;
import systole.synchronization.remote.entities.IdentityCardTypeRemote;

/**
 *
 * @author jmj
 */
public class IdentityCardTypeSyncBrokerDB extends BrokerDB implements IdentityCardTypeSyncBroker {

    public void saveIdentityCardTypeRemote(IdentityCardTypeRemote IdentityCardTypeRemote) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving remote identity card");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(IdentityCardTypeRemote);
            this.logger.logDebug("remote identity card saved");
        } catch (HibernateException e) {
            this.logger.logError("error on save identity card remote, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron guardar los cambios", e.fillInStackTrace());
        }
    }

    public List<IdentityCardTypeRemote> getIdentityCardTypeToUpdate() throws ExceptionDAO {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<IdentityCardType> getIdentityCardTypeToUpload() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting identity card to upload");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            String hql = "select i from IdentityCardTypeRemote ir right join ir.cardType i where ir=null";
            Query query = currentSession.createQuery(hql);
            @SuppressWarnings("unchecked")
            List<IdentityCardType> list = query.list();
            this.logger.logDebug("get successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get identity card to upload, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los documentos", e.fillInStackTrace());
        }
    }

    public IdentityCardType getIdentityCardTypeByRemoteId(int remoteId) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting identity card to upload");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    IdentityCardTypeRemote.class).add(Restrictions.eq("remoteId", remoteId)).setMaxResults(1);
            IdentityCardTypeRemote identityCardTypeRemote = (IdentityCardTypeRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (identityCardTypeRemote != null ? identityCardTypeRemote.getCardType() : null);
        } catch (HibernateException e) {
            this.logger.logError("error on get identity card by remote id, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los documentos", e.fillInStackTrace());
        }
    }

    public IdentityCardTypeRemote getIdentityCardTypeRemoteFromCardType(IdentityCardType cardType) throws ExceptionDAO {
          try {
            this.logger.logDebug("getting identity card remote by local identity card");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    IdentityCardTypeRemote.class).add(Restrictions.eq("cardType", cardType)).setMaxResults(1);
            IdentityCardTypeRemote identityCardTypeRemote = (IdentityCardTypeRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return identityCardTypeRemote;
        } catch (HibernateException e) {
            this.logger.logError("error on get remote identity card, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los documentos", e.fillInStackTrace());
        }
    }

    public int getLastRemotIdentityCardTypeIdSynchronized() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting last remote identity card sync");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    IdentityCardTypeRemote.class).addOrder(Order.desc("remoteId")).setMaxResults(1);
            IdentityCardTypeRemote identityCardTypeRemote = (IdentityCardTypeRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (identityCardTypeRemote != null ? identityCardTypeRemote.getRemoteId() : -1);
        } catch (HibernateException e) {
            this.logger.logError("error on get last remote identity card synchronized, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los documentos", e.fillInStackTrace());
        }
    }
}
